package com.xiaoyu.izf.controller;

import cn.hutool.json.JSONUtil;
import com.xiaoyu.izf.annotation.RequestAuth;
import com.xiaoyu.izf.model.Region;
import com.xiaoyu.izf.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地区
 */
@RestController
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    /**
     * 地区列表，要查省份regionId=1
     * @param regionId
     * @return
     */
    @RequestMapping("/list")
    @RequestAuth
    public List<Region> list(int regionId){
        return regionService.findByParentId(regionId);
    }

    @RequestMapping("/list_all")
//    @RequestAuth
    public List<Region> listAll(){
        Map<String,Object> map = new HashMap<>();

        List<Region> provinces = regionService.findByParentId(1);
        for (Region region:provinces){
            List<Region> cities = regionService.findByParentId(region.getRegionId());
            region.setChildren(cities);
            for (Region region1:cities){
                List<Region> counties = regionService.findByParentId(region1.getRegionId());
                region1.setChildren(counties);
            }
        }

        return provinces;
    }
}
